package com.lc.user.express.httpserver;

import android.text.TextUtils;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.lc.user.express.model.AddrModel;
import com.lc.user.express.model.OrderModel;
import com.lc.user.express.utils.PublicMethod;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/11.订单列表
 */
@HttpInlet(Net.GET_ORDER_LIST)
public class GetOrderList extends ZJDBAsyGet<GetOrderList.Info> {
    public String userid;
    public int page;

    public GetOrderList(String userid,int page,AsyCallBack listener) {
        super(listener);
        this.userid = userid;
        this.page = page;
    }

    @Override
    protected Info parser(JSONObject object) {
        if (object.optInt("message") == 1) {
            Info info = new Info();
            info.ispage = object.optInt("ispage");
            info.nextpage = object.optInt("nextpage");
            JSONArray array = object.optJSONArray("list");
            if(array != null){
                for (int i = 0; i<array.length(); i++){
                    JSONObject jsonObject = array.optJSONObject(i);
                    OrderModel model = new OrderModel();
                    model.setoId(jsonObject.optString("orderid"));
                    model.setoDatetype(jsonObject.optString("datetype"));
                    model.setoTime(jsonObject.optString("posttime"));
                    model.setoDriverHead(Net.SERVER + jsonObject.optString("avatar"));
                    model.setoDriver(jsonObject.optString("name"));
                    model.setoStatus(jsonObject.optInt("checkinfo"));
                    model.setoFee(jsonObject.optString("account"));
                    model.setoCid(jsonObject.optString("cid"));
                    model.setoCname(jsonObject.optString("cname"));
                    model.setoMobile(jsonObject.optString("mobile"));
                    JSONArray jsonArray = jsonObject.optJSONArray("addresslist");
                    List<AddrModel> addrlist = new ArrayList<>();
                    for(int j=0;j<jsonArray.length();j++){
                        AddrModel addrModel = new AddrModel();
                        JSONObject object1 = jsonArray.optJSONObject(j);
                        addrModel.setAddr(object1.optString("address"));
                        addrModel.setName(object1.optString("name"));
                        addrModel.setMobile(object1.optString("mobile"));
                        addrModel.setPoint(object1.optString("point"));
                        if(!TextUtils.isEmpty(object1.optString("point"))){
                            String[] s = object1.optString("point").split(",");
                            addrModel.setLng(PublicMethod.strToDouble(s[1]));
                            addrModel.setLat(PublicMethod.strToDouble(s[0]));
                        }
                        addrlist.add(addrModel);
                    }
                    model.setAddrlist(addrlist);
                    info.dataList.add(model);
                }

            }
            return info;
        }
        return null;
    }

    public static class Info {
        public List<OrderModel> dataList = new ArrayList<>();
        public int ispage;
        public int nextpage;

    }
}
